{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using DataFrames and Plots in a .NET Interactive Notebook\n",
    "\n",
    "For the full article see: https://swharden.com/blog/2022-05-01-dotnet-dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Data.Analysis, 0.19.1</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget:Microsoft.Data.Analysis\"\n",
    "using Microsoft.Data.Analysis;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [],
   "source": [
    "// load sample data\n",
    "string[] names = { \"Oliver\", \"Charlotte\", \"Henry\", \"Amelia\", \"Owen\" };\n",
    "int[] ages = { 23, 19, 42, 64, 35 };\n",
    "double[] heights = { 1.91, 1.62, 1.72, 1.57, 1.85 };\n",
    "\n",
    "DataFrameColumn[] columns = {\n",
    "    new StringDataFrameColumn(\"Name\", names),\n",
    "    new PrimitiveDataFrameColumn<int>(\"Age\", ages),\n",
    "    new PrimitiveDataFrameColumn<double>(\"Height\", heights),\n",
    "};\n",
    "\n",
    "DataFrame df = new(columns);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table id=\"table_637870780232683108\"><thead><tr><th><i>index</i></th><th>Name</th><th>Age</th><th>Height</th></tr></thead><tbody><tr><td><i><div class=\"dni-plaintext\">0</div></i></td><td>Oliver</td><td><div class=\"dni-plaintext\">23</div></td><td><div class=\"dni-plaintext\">1.91</div></td></tr><tr><td><i><div class=\"dni-plaintext\">1</div></i></td><td>Charlotte</td><td><div class=\"dni-plaintext\">19</div></td><td><div class=\"dni-plaintext\">1.62</div></td></tr><tr><td><i><div class=\"dni-plaintext\">2</div></i></td><td>Henry</td><td><div class=\"dni-plaintext\">42</div></td><td><div class=\"dni-plaintext\">1.72</div></td></tr><tr><td><i><div class=\"dni-plaintext\">3</div></i></td><td>Amelia</td><td><div class=\"dni-plaintext\">64</div></td><td><div class=\"dni-plaintext\">1.57</div></td></tr><tr><td><i><div class=\"dni-plaintext\">4</div></i></td><td>Owen</td><td><div class=\"dni-plaintext\">35</div></td><td><div class=\"dni-plaintext\">1.85</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "// visualize the dataframe\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>ScottPlot, 4.1.42</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "// install ScottPlot and register its display type\n",
    "#r \"nuget:ScottPlot\"\n",
    "using Microsoft.DotNet.Interactive.Formatting;\n",
    "Formatter.Register(typeof(ScottPlot.Plot), (plt, writer) => \n",
    "    writer.Write(((ScottPlot.Plot)plt).GetImageHTML()), HtmlFormatter.MimeType);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\"></img>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "// get data as double arrays\n",
    "double[] ages = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df[\"Age\"][x])).ToArray();\n",
    "double[] heights = Enumerable.Range(0, (int)df.Rows.Count).Select(x => Convert.ToDouble(df[\"Height\"][x])).ToArray();\n",
    "\n",
    "// create and display a plot\n",
    "var plt = new ScottPlot.Plot(400, 300);\n",
    "plt.AddScatter(ages, heights);\n",
    "plt.XLabel(\"Age\");\n",
    "plt.YLabel(\"Height\");\n",
    "plt"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "9.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
